/**
 * creation: 22-apr-2008
 */
package org.jfonia.structure;

import org.jfonia.connect.Callable;
import org.jfonia.connect.BasicNode;
import org.jfonia.connect.Value;
import org.jfonia.connect.logic.IfElse;

/**
 * @author wijnand.schepens@gmail.com
 *
 */
public class CombinedSlice implements Slice
{
	private Slice master;
	private Slice slave;
	
	private Value<Boolean> proceedCondition;
	private Callable proceeder;
	
	public CombinedSlice(Slice master, Slice slave, String refId)
	{
		this.master = master;
		this.slave = slave;
		
		BasicNode prop = new BasicNode();
		prop.addCallTarget(master.getProceeder());
		prop.addCallTarget(ForwardTo.createForwardTo(slave, refId, master.getValue()));

		this.proceedCondition = master.getProceedCondition();
		this.proceeder = new IfElse(proceedCondition, prop);
		
		// alternative: attach slave forwarder to master.getValue()
	}

	public Value<Integer> getValue()
	{
		return master.getValue();
	}

	public Value<Integer> getLookaheadValue()
	{
		return master.getLookaheadValue();
	}

	public Callable getProceeder()
	{
		return this.proceeder;
	}

	public Value<Boolean> getProceedCondition()
	{
		return this.proceedCondition;
	}
	
	public Value getProperty(String id)
	{
		if (master.hasProperty(id))
			return master.getProperty(id);
		else
			return slave.getProperty(id);	// throws
	}

	public Value getLookaheadProperty(String id)
	{
		if (master.hasProperty(id))
			return master.getLookaheadProperty(id);
		else
			return slave.getLookaheadProperty(id);	// throws
	}

	public boolean hasProperty(String id)
	{
		return master.hasProperty(id) || slave.hasProperty(id);
	}

	
}
